<template>
	<view class="content">
		<!-- <view class="navbar">
			<view 
				v-for="(item, index) in navList" :key="index" 
				class="nav-item nav-item-btn" 
				:class="{current: tabCurrentIndex === index, nav_item_reddot: item.reddot}"
				@click="tabClick(index)"
			>
				{{item.text}}
			</view>
		</view> -->
		<uni-notice-bar :scrollable="true" :single="true" text="医护人员重大福音, 本系统提供帮学服务,欢迎使用!" />
		<view class="uni-box-head">
			<uni-title type="h1" color="#027fff" align="center" title="学课机器猫"></uni-title>
		</view>
		<view class="uni-box">
			<uni-title class="h3" type="h3" title="介绍"></uni-title>
		</view>
		<view class="">
			<uni-title class="h5" type="h5" color="#666" title="- 我们提供各种的代学代考服务"></uni-title>
			<uni-title class="h5" type="h5" color="#666" title="- 超低价代学代考, 只需要点几下"></uni-title>
		</view>
		<view class="uni-box">
			<uni-title class="h3" type="h3" title="操作步骤:"></uni-title>
		</view>
		<view class="example-body">
			<uni-steps :options="list1" active-color="#007AFF" />
		</view>
		<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem,tabIndex) in navList" :key="tabIndex">
				<scroll-view 
					class="list-scroll-content" 
					scroll-y
					@scrolltolower="loadData"
				>
					<!-- 空白页 -->
					<empty v-if="tabItem.loaded === true && tabItem.brocastList.length === 0"></empty>
					
					<!-- 信息列表 -->
					<view 
						v-for="(item,index) in tabItem.brocastList" :key="index"
						class="course-item"
					>
						<view class="course-content" @click="navToDetailPage(item.id, tabIndex, index)">
							<view class="img-wrapper" v-if="item.pic">
								<image class="pic" :src="item.pic"></image>
							</view>
							<view class="uni-flex uni-row banner" style="-webkit-justify-content: space-between;justify-content: space-between;">
								<text class="title" >{{item.name}}</text>
								<text class="price"><span class="money">{{item.need_money}}</span>元</text>
							</view>
							<text class="introduce">
								{{item.describe}}
							</text>
						</view>
					</view>
					 
					<uni-load-more :status="tabItem.loadingType"></uni-load-more>
					
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template> 

<script>
	import uniNoticeBar from '@/components/uni-notice-bar/uni-notice-bar.vue';
	import uniTitle from '@/components/uni-title/uni-title.vue';
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import uniSteps from '@/components/uni-steps/uni-steps.vue';
	
	import request_async from '../../common/request_async.js';
	import empty from "@/components/empty";
	import { dateUtils } from '@/common/util';
	import { inArray } from '@/common/common';
	export default {
		components: {
			uniLoadMore,
			uniNoticeBar,
			uniTitle,
			uniSteps,
			empty
		},
		data() {
			return {
				readedIds_key : '',
				readedIds_value : [],
				tabCurrentIndex: 0,
				navList: [
					{
						type: 0,
						reddot : false,
						text: '',
						loadingType: 'more',
						brocastList: []
					}
				],
				list1: [{
					title: '选中课程'
				}, {
					title: '手机登录注册'
				}, {
					title: '报名，填写平台账号与密码'
				}, {
					title: '点击开始'
				}]
			};
		},
		computed: {
		},
		onShow() {
			uni.setNavigationBarTitle({
			    title: '学课机器猫,公需课、党课，一键帮学，免费，少收费，欢迎报名'
			});
		},
		onLoad(options){
			
			this.tabCurrentIndex = options.type ? options.type : 0;
			// #ifndef MP
			this.loadData()
			// #endif
			// #ifdef MP
			if(options.type == 0){
				this.loadData()
			}
			// #endif
			this.loadAll();
		},
		 
		methods: {
			loadAll(){
				//获取已读id数组
				var pages = getCurrentPages();
				var route = (pages[pages.length - 1]).route;
				// console.log(route)
				for(var index = 0; index < this.navList.length; index++){
					var oneRed = this.loadData('', index);
				}
			},
			//获取信息列表
			async loadData(source, nowIndex){
				//这里是将信息挂载到tab列表下
				let index = nowIndex !== undefined ? nowIndex : this.tabCurrentIndex;
				console.log('loadData index : '+index)
				let navItem = this.navList[index];
				let type = navItem.type;
				let page = navItem.page ? navItem.page  : 1;
				
				if(navItem.loaded === true){
					//tab切换只有第一次需要加载数据
					return;
				}
				if(navItem.loadingType === 'loading'){
					//防止重复加载
					return;
				}
				
				navItem.loadingType = 'loading';
				
				var res = await request_async({
					name: 'course.index',
					method: 'POST',
					data: {page : page, type : type}
				})
				if(res.code != 0){
					return false;
				}
				var pageInfo = res.data;
				let brocastList = pageInfo.data.filter(item=>{
					//演示数据所以自己进行状态筛选
					return true
				});
				
				var isReddot = false;
				brocastList.forEach(item=>{
					navItem.brocastList.push(item);
				})
				
				//判断是否还有数据， 有改为 more， 没有改为noMore 
				if(pageInfo.last_page > page){
					navItem.page = page+1;
					navItem.loadingType = 'more';
				}else{
					navItem.loadingType = 'noMore';
					//loaded新字段用于表示数据加载完毕，如果为空可以显示空白页
					this.$set(navItem, 'loaded', true);
				}
				return true;
			}, 
			navToDetailPage(id, tabIndex, index) {
				uni.navigateTo({
					url: `/pages/course/detail?id=${id}`
				})
			},
			//swiper 切换
			changeTab(e){
				this.tabCurrentIndex = e.target.current;
				this.loadData('tabChange');
			},
			//顶部tab点击
			tabClick(index){
				this.tabCurrentIndex = index;
			},

		},
	}
</script>

<style lang="scss">
	page, .content{
		background: $page-color-base;
		height: 100%;
	}
	
	.swiper-box{
		height: calc(100% - 40px);
	}
	.list-scroll-content{
		height: 100%;
	}
	.navbar{
		display: flex;
		height: 40px;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0,0,0,.06);
		position: relative;
		z-index: 10;
		.nav-item{
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 15px;
			color: $font-color-dark;
			position: relative;
			&.current{
				color: $base-color;
				&:after{
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 2px solid $base-color;
				}
			}
		}
	}
	.nav-item-btn {
		position: relative;
		width: auto;
		margin: 0 2px;
		word-break: keep-all;
		white-space: pre;
		cursor: pointer;
	}
	.nav_item_reddot:after {
		content: attr(badge-text);
		// position: absolute;
		background-color: red;
		color: #fff;
		width: 18px;
		height: 18px;
		line-height: 18px;
		border-radius: 18px;
		overflow: hidden;
		-webkit-transform: scale(.5) translate(40%,-40%);
		transform: scale(.5) translate(40%,-40%);
		-webkit-transform-origin: 100% 0;
		transform-origin: 100% 0;
	}
	.title_reddot:after {
		content: attr(badge-text);
		background-color: red;
		color: #fff;
		width: 18px;
		height: 18px;
		float: right;
		line-height: 18px;
		border-radius: 18px;
		overflow: hidden;
		-webkit-transform: scale(.5) translate(40%,-40%);
		transform: scale(.5) translate(40%,-40%);
		-webkit-transform-origin: 100% 0;
		transform-origin: 100% 0;
	}
	.uni-swiper-item{
		height: auto;
	}
	.course-item{
		display: flex;
		flex-direction: column;
		padding-left: 30upx;
		background: #fff;
		margin-top: 16upx;
		.course-content {
			width: 710upx;
			padding: 0 24upx;
			background-color: #fff;
			border-radius: 4upx;
		}
		.banner {
			flex-grow: 2;
			display: flex;
			height: 90upx;
			font-size: 30rpx;
			align-items: center;
			.title {
				color: #303133;
			}
			.price {
				float: right;
				font-weight: bold;
			}
			.money {
				color: red;
			}
		}
		.img-wrapper {
			width: 100%;
			height: 260upx;
			position: relative;
		}
		.pic {
			display: block;
			width: 100%;
			height: 100%;
			border-radius: 6upx;
		}
		.cover {
			display: flex;
			justify-content: center;
			align-items: center;
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;
			background-color: rgba(0, 0, 0, .5);
			font-size: 36upx;
			color: #fff;
		}
		.introduce {
			display: inline-block;
			padding: 16upx 0;
			font-size: 28upx;
			line-height: 38upx;
		}
		.bot {
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 90upx;
			font-size: 24upx;
			color: #707070;
			position: relative;
		}
		.more-icon {
			font-size: 32upx;
		}
		
		
	}
	
	
	/* load-more */
	.uni-load-more {
		display: flex;
		flex-direction: row;
		height: 80upx;
		align-items: center;
		justify-content: center
	}
	
	.uni-load-more__text {
		font-size: 28upx;
		color: #999
	}
	
	.uni-load-more__img {
		height: 24px;
		width: 24px;
		margin-right: 10px
	}
	
	.uni-load-more__img>view {
		position: absolute
	}
	
	.uni-load-more__img>view view {
		width: 6px;
		height: 2px;
		border-top-left-radius: 1px;
		border-bottom-left-radius: 1px;
		background: #999;
		position: absolute;
		opacity: .2;
		transform-origin: 50%;
		animation: load 1.56s ease infinite
	}
	
	.uni-load-more__img>view view:nth-child(1) {
		transform: rotate(90deg);
		top: 2px;
		left: 9px
	}
	
	.uni-load-more__img>view view:nth-child(2) {
		transform: rotate(180deg);
		top: 11px;
		right: 0
	}
	
	.uni-load-more__img>view view:nth-child(3) {
		transform: rotate(270deg);
		bottom: 2px;
		left: 9px
	}
	
	.uni-load-more__img>view view:nth-child(4) {
		top: 11px;
		left: 0
	}
	
	.load1,
	.load2,
	.load3 {
		height: 24px;
		width: 24px
	}
	
	.load2 {
		transform: rotate(30deg)
	}
	
	.load3 {
		transform: rotate(60deg)
	}
	
	.load1 view:nth-child(1) {
		animation-delay: 0s
	}
	
	.load2 view:nth-child(1) {
		animation-delay: .13s
	}
	
	.load3 view:nth-child(1) {
		animation-delay: .26s
	}
	
	.load1 view:nth-child(2) {
		animation-delay: .39s
	}
	
	.load2 view:nth-child(2) {
		animation-delay: .52s
	}
	
	.load3 view:nth-child(2) {
		animation-delay: .65s
	}
	
	.load1 view:nth-child(3) {
		animation-delay: .78s
	}
	
	.load2 view:nth-child(3) {
		animation-delay: .91s
	}
	
	.load3 view:nth-child(3) {
		animation-delay: 1.04s
	}
	
	.load1 view:nth-child(4) {
		animation-delay: 1.17s
	}
	
	.load2 view:nth-child(4) {
		animation-delay: 1.3s
	}
	
	.load3 view:nth-child(4) {
		animation-delay: 1.43s
	}
	
	@-webkit-keyframes load {
		0% {
			opacity: 1
		}
	
		100% {
			opacity: .2
		}
	}
</style>
